import 'package:jhjj/parse_jhjj.dart';


List<VolumeResult> purgeList( List<VolumeResult> list ) {
    for (var item in list) {
        if (item.executedBuy > item.executedSell) {
            item.executedSell = 0;
        } else {
            item.executedBuy = 0;
        }
        if (item.unexecutedBuy > item.unexecutedSell) {
            item.unexecutedSell = 0;
        } else {
            item.unexecutedBuy = 0;
        }
    }
    return list;
}

bool isLinearTrendIncreasing(List<int> arr) {
    if (arr.length < 2) return false;

    double sumX = 0, sumY = 0, sumXY = 0, sumX2 = 0;
    for (int i = 0; i < arr.length; i++) {
        sumX += i; // X是索引（0,1,2,...）
        sumY += arr[i]; // Y是数据值
        sumXY += i * arr[i];
        sumX2 += i * i;
    }

    // 计算斜率（最小二乘法）
    double n = arr.length.toDouble();
    double slope = (n * sumXY - sumX * sumY) / (n * sumX2 - sumX * sumX);

    return slope > 0; // 斜率为正表示趋势向上
}

bool checkVolumeTrendIncreasing(List<VolumeResult> volumeList,
    {required bool checkBuy, required bool checkExecuted}) {
    var arr = <int>[];
    for (var vol in volumeList) {
        if (checkExecuted) {
            if (checkBuy) {
                arr.add(vol.executedBuy);
            } else {
                arr.add(vol.executedSell);
            }
        } else {
            if (checkBuy) {
                arr.add(vol.unexecutedBuy);
            } else {
                arr.add(vol.unexecutedSell);
            }
        }
    }
    return isLinearTrendIncreasing(arr);
}